﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Lottery.App;
using Lottery.App.BLL;
using System.Threading;
using System.Diagnostics;

namespace UI
{
    public partial class TJDT : Form
    {
        private GameMode _gm;
        private LotteryManager _manager = new LotteryManager();
        private string _value;
        private List<string> _values;

        private delegate void HideLoadding();

        public TJDT(GameMode gm, string title)
        {
            InitializeComponent();

            this.Text = title + String.Format("    统计日期: {0}", GlobalData.Records[GlobalData.Records.Count - 1].IssueNum);
            dgv_list.AutoGenerateColumns = false;
            _gm = gm;
        }

        private void TJ_Load(object sender, EventArgs e)
        {
            LoadData(OrderBy.ID);
        }

        public void LoadData(OrderBy order)
        {
            dgv_list.DataSource = _manager.List(_gm, order);

            dgv_list.Rows[0].Selected = false;
        }

        

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgv_list.SelectedRows.Count > 0)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;

                _value = String.Format("{0} {1}",
                    dgv_list.SelectedRows[0].Cells[0].Value,
                    dgv_list.SelectedRows[0].Cells[1].Value
                );

                LoadingManager.Show();

                Thread t = new Thread(ExportMin);
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                MessageBox.Show("请先选中要导出的投注方案", "提示");
            }
        }
        void Do_LoadData()
        {
            string path = new ExportManager().Export(_value, _gm);

            Process.Start(path);

            Invoke(new HideLoadding(HideLoading));
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (dgv_list.SelectedRows.Count > 0)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;

                _value = String.Format("{0} {1}",
                    dgv_list.SelectedRows[0].Cells[0].Value,
                    dgv_list.SelectedRows[0].Cells[1].Value
                );

                LoadingManager.Show();

                Thread t = new Thread(Do_LoadData);
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                MessageBox.Show("请先选中要导出的投注方案", "提示");
            }
        }

        void ExportMin()
        {
            string path = new ExportManager().ExportMin(_value, _gm);

            Process.Start(path);

            Invoke(new HideLoadding(HideLoading));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dgv_list.SelectedRows.Count > 20)
            {
                MessageBox.Show("一次性导出不得大于20条!", "提示");
                return;
            }

            switch (_gm)
            {
                case GameMode.N2:
                case GameMode.N3:
                case GameMode.N4:
                case GameMode.N5:
                case GameMode.D1T2:
                case GameMode.B4Z3:
                case GameMode.B5Z4:
                case GameMode.B6Z5:
                    break;
                default:
                    MessageBox.Show("目前只支持 任二、任三、任四、任五、一胆二拖、买四中三、买五中四、买六中五 的多列导出!", "提示");
                    return;
            }

            if (dgv_list.SelectedRows.Count > 0)
            {
                button2.Enabled = false;
                button5.Enabled = false;
                button1.Enabled = false;

                _values = new List<string>(dgv_list.SelectedRows.Count);
                for (int i = 0; i < dgv_list.SelectedRows.Count; i++)
                {
                    _values.Add(String.Format("{0} {1}",
                        dgv_list.SelectedRows[i].Cells[0].Value,
                        dgv_list.SelectedRows[i].Cells[1].Value
                    ));
                }
                _values.Sort();

                LoadingManager.Show();

                Thread t = new Thread(ExportLimit);
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                MessageBox.Show("请先选中要导出的投注方案", "提示");
            }
        }
        void ExportLimit()
        {
            string path = new ExportManager().ExportLimit(_values, _gm);

            Process.Start(path);

            Invoke(new HideLoadding(HideLoading));
        }

        void HideLoading()
        {
            button1.Enabled = true;
            button2.Enabled = true;
            button5.Enabled = true;

            LoadingManager.Hide();
        }



        #region 列排序

        private void btnID_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.ID);

            ResetColumnColor(0);
        }

        private void btnOmit_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.CurrentOmit);

            ResetColumnColor(2);
        }

        private void btnMaxOmit_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.MaxOmit);

            ResetColumnColor(3);
        }

        private void btnQH_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.Date);

            ResetColumnColor(4);
        }


        private void ResetColumnColor(int column)
        {
            dgv_list.Columns[0].DefaultCellStyle.ForeColor = Color.Black;
            dgv_list.Columns[1].DefaultCellStyle.ForeColor = Color.Black;
            dgv_list.Columns[2].DefaultCellStyle.ForeColor = Color.Black;
            dgv_list.Columns[3].DefaultCellStyle.ForeColor = Color.Black;

            dgv_list.Columns[column].DefaultCellStyle.ForeColor = Color.Blue;
        }
        #endregion






    }
}
